package com.viewpager2.teach;

import android.os.Bundle;
import android.os.Handler;
import android.view.animation.ScaleAnimation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import com.viewpager2.teach.adapter.ViewPager2OneAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Creator:Yx
 * Time:2020/8/5 10:18
 * Description:ViewPager+RecyclerView
 */
public class TextOneActivity extends AppCompatActivity {
    private ViewPager2 viewpager2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_one);
        //设置ViewPager2
        initViewPager2();
    }

    private void initViewPager2() {
        viewpager2 = findViewById(R.id.public_viewPager2);
        //默认是水平方向ORIENTATION_HORIZONTAL,垂直是ORIENTATION_VERTICAL
        viewpager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //设置页与页的间距
        viewpager2.setPageTransformer(new MarginPageTransformer(15));
        //设置翻页动画
        //CompositePageTransformer transformer = new CompositePageTransformer();
        //设置Adapter
        ViewPager2OneAdapter adapter = new ViewPager2OneAdapter(this, viewpager2);
        viewpager2.setAdapter(adapter);
        //设置翻页监听
        viewpager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
        //模拟延迟数据设置
        new Handler().postDelayed(() -> {
            List<String> list = new ArrayList<>();
            list.add("页面一");
            list.add("页面二");
            list.add("页面三");
            list.add("页面四");
            adapter.setViewPager2Data(list);
        }, 1000);

    }


}
